angular.module('driver')

.directive('map', function() {
  return {
    restrict: 'E',
    scope: {
      onCreate: '&',
      location: '='
    },
    link: function($scope, $element, $attr) {
      function initialize() {
        //console.log($attr);
        var loc = $scope.location;
        if (!loc.lng || !loc.lat) {
          loc = {
            lng: 116.117582,
            lat: 24.299112
          }
        }

        var mapOptions = {
          animateEnable:true,
          zoomEnable:true,
          doubleClickZoom:true,
          jogEnable:true,
          touchZoom:true,
          scrollWheel:true,
          view: new AMap.View2D({
            center: new AMap.LngLat(loc.lng, loc.lat),
            zoom: 14,
            rotation: 0
          })
        };
        //        console.log($element[0], $scope, $attr);
        var map = new AMap.Map($element[0].id, mapOptions);
        //console.log(map);
        $scope.onCreate({
          map: map
        });
        window.map = map;
        // Stop the side bar from dragging when mousedown/tapdown on the map
        /*
        google.maps.event.addDomListener($element[0], 'mousedown', function (e) {
          e.preventDefault();
          return false;
        });
*/
      }
      window.onload = initialize();
      /*
      if (document.readyState === "complete") {
        initialize();
      } else {
        google.maps.event.addDomListener(window, 'load', initialize);
      }*/
    }
  }
});
